﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MisClient
{
    public partial class FrmConfirmMoney2 : BaseForm
    {
        private IBLL.IDepart departBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDepart();
        private IBLL.ISacf sacfBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSacf();
        private IBLL.ICustomer cusBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteCustomer();
        private IBLL.IFkType typeBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteFkType();
        private IBLL.ICusDep cusDepBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteCusDep();

        public FrmConfirmMoney2()
        {
            InitializeComponent();
        }

        private void FrmInvoiceInvalid_Load(object sender, EventArgs e)
        {
            this.openStarDate.Format = DateTimePickerFormat.Custom;
            this.openStarDate.CustomFormat = " ";
            this.openEndDate.Format = DateTimePickerFormat.Custom;
            this.openEndDate.CustomFormat = " ";
            this.txtDepCode.Text = accountUser.DeptCode;

            this.expireStarDate.Value = DateTime.Now.Date.AddDays(-10);
            this.expireStarDate.Focus();
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void openStarDate_ValueChanged(object sender, EventArgs e)
        {
            this.openStarDate.Format = DateTimePickerFormat.Long;
            this.openStarDate.CustomFormat = null;
        }

        private void openEndDate_ValueChanged(object sender, EventArgs e)
        {
            this.openEndDate.Format = DateTimePickerFormat.Long;
            this.openEndDate.CustomFormat = null;
        }

        private void expireStarDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.expireEndDate.Focus();
            }
        }

        private void expireEndDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtDepCode.Focus();
            }
        }

        private void txtCusCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtCusCode.Text.Trim()))
                {
                    MisModel.Customer customer = cusBLL.GetById(this.txtCusCode.Text.Trim());
                    if (customer == null)
                    {
                        MessageBox.Show("无此代码或不属于本部分！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtCusCode.Focus();
                        return;
                    }
                    this.txtBoxCus.Text = customer.Customer_;
                }
                this.txtCusDepCode.Focus();
            }
        }

        private void txtInvoiceNum_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtInvoiceCode.Focus();
            }
        }

        private void txtInvoiceCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtCusShopCode.Focus();
            }
        }

        private void txtDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
                {
                    MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                if (this.txtDepCode.Text.Trim().Length < 2)
                {
                    MessageBox.Show("部门代码长度为2！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
                if (depart == null)
                {
                    MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                this.txtBoxDep.Text = depart.DepName;
                this.txtCusCode.Focus();
            }
        }

        private void txtCusDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtCusDepCode.Text.Trim()))
                {
                    MisModel.CusDep cusDep = cusDepBLL.GetById(this.txtCusCode.Text.Trim());
                    if (cusDep == null)
                    {
                        MessageBox.Show("客户部门代码不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtCusDepCode.Focus();
                        return;
                    }
                    else
                    {
                        this.txtBoxCusDep.Text = cusDep.Depart;
                    }
                }
                this.txtInvoiceNum.Focus();
            }
        }

        private void txtCusShopCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtCusShopCode.Text.Trim()))
                {
                    MisModel.FKType fkType = typeBLL.GetById(this.txtCusShopCode.Text.Trim());
                    if (fkType == null)
                    {
                        MessageBox.Show("无此代码或不属于此结算类型！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtCusShopCode.Focus();
                        return;
                    }
                    this.txtBoxCusDep.Text = fkType.FkType;
                }
                this.btnOk.Focus();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
            {
                MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
            if (depart == null)
            {
                MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            StringBuilder sb = new StringBuilder();
            this.dataGridView1.Rows.Clear();
            sb.Append(" m.dep_id=@depId");
            dictionary.Add("@depId", this.txtDepCode.Text.Trim());
            if (!string.IsNullOrEmpty(this.openStarDate.Text.ToString().Trim()))
            {
                sb.Append(" and m.date>=@startDate");
                dictionary.Add("@startDate", this.openStarDate.Value.ToShortDateString());
            }
            if (!string.IsNullOrEmpty(this.openEndDate.Text.ToString().Trim()))
            {
                sb.Append(" and m.date<=@endDate");
                DateTime time = DateTime.Parse(this.openEndDate.Value.ToShortDateString());
                time = time.Date.AddDays(1);
                dictionary.Add("@endDate", time.ToShortDateString());
            }
            sb.Append(" and m.d_kp>=@gqStartDate and m.d_kp<=@gqEndDate");
            dictionary.Add("@gqStartDate", this.expireStarDate.Value.ToShortDateString());
            DateTime time1 = DateTime.Parse(this.expireEndDate.Value.ToShortDateString());
            time1 = time1.Date.AddDays(1);
            dictionary.Add("@gqEndDate", time1.ToShortDateString());
            if (!string.IsNullOrEmpty(this.txtCusCode.Text.Trim()))
            {
                sb.Append(" and m.cus_id=@cusId");
                dictionary.Add("@cusId", this.txtCusCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCusDepCode.Text.Trim()))
            {
                sb.Append(" and m.cus_dep=@cusDep");
                dictionary.Add("@cusDep", this.txtCusDepCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCusShopCode.Text.Trim()))
            {
                sb.Append(" and m.type_js=@typeJs");
                dictionary.Add("@typeJs", this.txtCusShopCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtInvoiceNum.Text.Trim()))
            {
                sb.Append(" and m.no_cus=@noCus");
                dictionary.Add("@noCus", this.txtInvoiceNum.Text.Trim());
            }
            DataTable dt = sacfBLL.GetBillingAll(sb.ToString(), dictionary);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    String[] DataRow = new String[34];
                    DataRow[1] = dt.Rows[i]["no_cus"].ToString();
                    DataRow[2] = dt.Rows[i]["no_js"].ToString();
                    DataRow[3] = dt.Rows[i]["js"].ToString();
                    DataRow[4] = dt.Rows[i]["hs"].ToString();
                    DataRow[5] = dt.Rows[i]["fs"].ToString();
                    DataRow[6] = dt.Rows[i]["tax"].ToString();
                    DataRow[7] = dt.Rows[i]["f3"].ToString();
                    DataRow[8] = dt.Rows[i]["d_kp"].ToString();
                    DataRow[9] = dt.Rows[i]["dep_id"].ToString();
                    DataRow[10] = dt.Rows[i]["cus_id"].ToString();
                    DataRow[11] = dt.Rows[i]["cus_dep"].ToString();
                    this.dataGridView1.Rows.Add(DataRow);
                }
                this.btnSave.Enabled = true;
                this.btnBy1.Enabled = true;
                this.btnBy2.Enabled = true;
                this.btnBy3.Enabled = true;
            }
            else
            {
                MessageBox.Show("查无符合条件的记录！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.Rows.Count > 0)
            {
                IList<MisEntity.Sacf> list = new List<MisEntity.Sacf>(this.dataGridView1.Rows.Count);
                DataGridViewRow dr = null;
                MisEntity.Sacf entity = null;
                int count = 0;
                for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    dr = this.dataGridView1.Rows[i];
                    Boolean dataSel = Convert.ToBoolean(dr.Cells["dataSel"].FormattedValue);
                    if (dataSel)
                    {
                        entity = new MisEntity.Sacf();
                        entity.NO_CUS = dr.Cells["data1"].Value.ToString();
                        entity.NO_JS = dr.Cells["data2"].Value.ToString();
                        entity.D_KP = DateTime.Parse(dr.Cells["data8"].Value.ToString());
                        entity.OPER = accountUser.UserCode;
                        list.Add(entity);
                        count++;
                    }
                }
                if (count <= 0)
                {
                    MessageBox.Show("无数据保存！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                sacfBLL.discardBilling(list);
                MessageBox.Show(string.Format("{0}条发票已保存完毕！", count), "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.btnSave.Enabled = false;
                this.btnBy1.Enabled = false;
                this.btnBy2.Enabled = false;
                this.btnBy3.Enabled = false;
            }
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            Point mousePoint = MousePosition;
            Point point = this.PointToClient(mousePoint);
            this.contextMenuStrip1.Show(this, point);
        }

        private void toolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }
    }
}
